//运行时配置文件，可以在这里扩展运行时的能力，比如修改路由、修改 render 方法等。
import router from "umi/router";
import { message } from "antd";
import { getQueryVariable } from './utils/index';
//简易 观察者模式， 发布订阅
window.PubSub = (function () {
  var eventObj = {};
  return {
    subscribe: function (event, fn) {
      eventObj[event] = fn;
    },
    publish: function (event, data) {
      if (eventObj[event]) eventObj[event](data);
    },
    off: function (event) {
      if (eventObj[event]) delete eventObj[event];
    }
  };
})();
//运行时配置文件
export const dva = {
  config: {
    onError(err) {
      err.preventDefault();
      console.error(err.message);
    },
  },
};

//渲染应用之前做权限校验，不通过则跳转到登录页
export function render(oldRender) {
  let access_key = getQueryVariable('access_key');
  let token = getQueryVariable('token');
  let system = getQueryVariable('system');
  if (token) {
    window.localStorage.setItem('access_key', access_key);
    window.localStorage.setItem('token', token);
    window.localStorage.setItem('system', system);
  } else {
    let a = window.localStorage.getItem('access_key');
    let t = window.localStorage.getItem('token');
    let s = window.localStorage.getItem('system');
    if (!t) {
      message.error('未获取到授权参数,请刷新页面重新登录!');
      router.push('/403');
    }
  }
  oldRender();
}
